package com.xhh.greynewsapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.xhh.greynewsapp.MainActivity;
import com.xhh.greynewsapp.R;
import com.xhh.greynewsapp.model.user.User;

import java.util.logging.LogManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;


/**
 * @description
 * @author: weiXhh
 * @create: 2020-05-27 15:00
 **/
public class RegisterActivity extends AppCompatActivity {

    @BindView(R.id.et_register_username)
    EditText etUsername;
    @BindView(R.id.et_register_pwd)
    EditText etPassword;
    @BindView(R.id.et_register_nickname)
    EditText etNickname;
    @BindView(R.id.btn_register)
    Button btnRegister;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);

        btnRegister.setOnClickListener(click->{
            String name = etUsername.getText().toString().trim();
            String pwd = etPassword.getText().toString().trim();
            String nick = etNickname.getText().toString().trim();

            if(name.equals("")){
                Toast.makeText(RegisterActivity.this, "用户名为空", Toast.LENGTH_SHORT).show();
            } else if(pwd.equals("")){
                Toast.makeText(RegisterActivity.this, "密码为空", Toast.LENGTH_SHORT).show();
            } else {
                final User user = new User();
                user.setUsername(name);
                user.setPassword(pwd);
                user.setNickname(nick);

                // Bmob注册用户
                user.signUp(new SaveListener<User>() {
                    @Override
                    public void done(User user, BmobException e) {
                        if (e == null) {
                            Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                            finish();
                        } else {
                            Log.e("RegisterActivity", "注册失败:" + e.toString());
                            Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

            }

        });
    }
}
